package com.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.blog.common.Result.PageResult;
import com.blog.common.basecontext.BaseContext;
import com.blog.common.exception.BaseException;
import com.blog.dto.CommentAddDto;
import com.blog.entity.Article;
import com.blog.entity.Comment;
import com.blog.entity.User;
import com.blog.mapper.CommentMapper;
import com.blog.service.IArticleService;
import com.blog.service.ICommentService;
import com.blog.service.IUserService;
import com.blog.vo.CommentGetByArticleIdVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {


    private final IArticleService articleService;

    private final IUserService userService;
    /**
     * 添加评论
     * @param commentAddDto
     */
    @Override
    public void addCommentToArticle(CommentAddDto commentAddDto) {
        Article article = articleService.getById(commentAddDto.getArticleId());
        if (article == null) {
            throw new BaseException("文章不存在");
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentAddDto, comment);
        LocalDateTime now = LocalDateTime.now();
        comment.setCreateTime(now);
        Long currentId = BaseContext.getCurrentId();
        comment.setUserId(currentId);
        //根据用户id查询用户
        User userServiceById = userService.getById(currentId);
        comment.setUserName(userServiceById.getName());
        comment.setUserImage(userServiceById.getImage());
        // 加入数据库
        save(comment);
        // 更新文章评论数量
        Long commentCount = article.getCommentCount();
        article.setCommentCount(commentCount + 1);
        articleService.updateById(article);
    }

    /**
     * 删除评论
     * @param commentId
     */
    @Override
    public void deleteComment(Long commentId) {
        // 获得当前登录用户id
        Long currentId = BaseContext.getCurrentId();
        // 获得评论
        Comment comment = getById(commentId);
        if (comment == null) {
            throw new BaseException("评论不存在");
        }
        // 判断是否为当前用户的评论
        if (!comment.getUserId().equals(currentId)) {
            throw new BaseException("不是当前用户的评论");
        }
        // 删除评论
        removeById(commentId);
        // 更新文章评论数量
        Article article = articleService.getById(comment.getArticleId());
        Long commentCount = article.getCommentCount();
        article.setCommentCount(commentCount - 1);
        articleService.updateById(article);
    }

    @Override
    public PageResult getCommentList(Long articleId, Integer pageNum, Integer pageSize) {
        //准备分页参数
        Page<Comment> page = new Page<>(pageNum, pageSize);
        // 准备查询条件
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        // 根据创建时间降序排列
        queryWrapper.eq(Comment::getArticleId, articleId).orderByDesc(Comment::getCreateTime);
        // 查询
        Page<Comment> commentPage = page(page, queryWrapper);
        // 封装返回结果
        PageResult pageResult = new PageResult();
        pageResult.setTotal(commentPage.getTotal());
        // 封装
        List<CommentGetByArticleIdVo> list = commentPage.getRecords().stream().map((item) -> {
            CommentGetByArticleIdVo commentGetByArticleIdVo = new CommentGetByArticleIdVo();
            BeanUtils.copyProperties(item, commentGetByArticleIdVo);
            return commentGetByArticleIdVo;
        }).toList();

        pageResult.setRecords(list);
        return pageResult;
    }
}
